package com.ruyue.csinstall.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

public class CsInstallEnv {
    private static final Log log = LogFactory.getLog(CsInstallEnv.class);
    private static Properties p = null;

    static{
        p = new Properties();
        try {
            p.load(new BufferedReader(new InputStreamReader(new FileInputStream(new File(".").getAbsolutePath() + File.separator + "cs.properties"), "UTF-8")));
        } catch (Exception e) {
            log.error("读取csinstall.properties出错了...,请检查一下当前目录是否存在该文件");
            e.printStackTrace();
        }
    }

    public static String getvalue(String key, String defaultStr) {
        return Utils.defaultIfEmpty(getvalue(key), defaultStr);
    }

    public static String getvalue(String key) {
        return p.getProperty(key);
    }

    public static List<String> getSamePrePropertyNames(String prefixStr){
        if(Utils.isEmpty(prefixStr)) return null;
        List<String> names = new ArrayList<String>();
        Set<String> stringPropertyNames = p.stringPropertyNames();
        for(String name : stringPropertyNames){
            if(name.startsWith(prefixStr)){
                names.add(name);
            }
        }
        Collections.sort(names);
        return names;
    }

    public static void main(String[] args) {
        System.out.println(getSamePrePropertyNames("12"));
    }
}

